package org.fuys.coder.application;

import org.fuys.coder.common.constants.ResultMessageConstants;
import org.fuys.coder.common.entity.Result;
import org.fuys.coder.common.exception.BusinessException;
import org.fuys.coder.common.holder.UserHolder;
import org.fuys.coder.domain.category.model.vo.CategoryVO;
import org.fuys.coder.domain.search.model.req.SearchReq;
import org.fuys.coder.domain.search.model.vo.AllSearchVO;
import org.fuys.coder.domain.search.model.vo.SearchResultVO;
import org.fuys.coder.domain.search.service.ISearchService;
import org.fuys.coder.domain.substance.model.vo.SubstanceVO;
import org.fuys.coder.domain.user.model.vo.repo.AuthorVO;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.application
 * @className: SearchController
 * @author: WangJing
 * @description: 司掌搜索业务的控制器
 * @date: 2024/8/15 12:09
 * @version: 1.0
 */
@RestController
@RequestMapping("/fuyscoder/search")
public class SearchController {

    @Resource
    private ISearchService searchService;

    @PostMapping("/substance")
    public Result searchSubstance(@RequestBody SearchReq searchReq){
        SearchResultVO<SubstanceVO> searchResultVO= null;
        try {
            final Integer userId = UserHolder.get();
            if(ObjectUtils.isEmpty(userId)){
                return Result.error(ResultMessageConstants.NO_USER_TOKEN);
            }
            searchResultVO = searchService.searchSubstance(searchReq);
        } catch (BusinessException e) {
            return Result.error(e.getMsg());
        }
        return Result.ok().data(searchResultVO);
    }

    @PostMapping("/author")
    public Result searchAuthor(@RequestBody SearchReq searchReq){
        SearchResultVO<AuthorVO> searchResultVO= null;
        try {
            final Integer userId = UserHolder.get();
            if(ObjectUtils.isEmpty(userId)){
                return Result.error(ResultMessageConstants.NO_USER_TOKEN);
            }
            searchResultVO = searchService.searchAuthor(searchReq);
        } catch (BusinessException e) {
            return Result.error(e.getMsg());
        }
        return Result.ok().data(searchResultVO);
    }

    @PostMapping("/category")
    public Result searchCategory(@RequestBody SearchReq searchReq){
        SearchResultVO<CategoryVO> searchResultVO= null;
        try {
            final Integer userId = UserHolder.get();
            if(ObjectUtils.isEmpty(userId)){
                return Result.error(ResultMessageConstants.NO_USER_TOKEN);
            }
            searchResultVO = searchService.searchCategory(searchReq);
        } catch (BusinessException e) {
            return Result.error(e.getMsg());
        }
        return Result.ok().data(searchResultVO);
    }

    @PostMapping("/multiple")
    public Result searchMultiple(@RequestBody SearchReq searchReq){
        SearchResultVO<AllSearchVO> searchResultVO= null;
        try {
            final Integer userId = UserHolder.get();
            if(ObjectUtils.isEmpty(userId)){
                return Result.error(ResultMessageConstants.NO_USER_TOKEN);
            }
            searchResultVO = searchService.searchMultiple(searchReq);
        } catch (BusinessException e) {
            return Result.error(e.getMsg());
        }
        return Result.ok().data(searchResultVO);
    }
}
